package com.jinyi.wwdeng.autodialer.data

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey


/**
 * 数据库实体
 */
@Entity(tableName = "tab_call_tasks")
data class CallTaskEntity(
    @PrimaryKey(autoGenerate = true)
    val id: Long = 0,

    @ColumnInfo(name = "phone_number")
    val phoneNumber: String,//号码

    @ColumnInfo(name = "status")
    var status: String, // 0: 待拨打, 1: 拨打中, 2: 待上传, 3: 上传中 4: 上传成功 5: 上传失败  6: 成功  -1: 失败

    @ColumnInfo(name = "create_time")
    var createTime: Long = System.currentTimeMillis(),//创建时间

    @ColumnInfo(name = "call_start_time")
    var callStartTime: Long = 0,//开始拨打时间

    @ColumnInfo(name = "call_end_time")
    var callEndTime: Long = 0,//挂断时间

    @ColumnInfo(name = "local_audio_path")
    var localAudioPath: String? = null,//录音文件本地路径

    @ColumnInfo(name = "server_audio_path")
    var serverAudioPath: String? = null,//录音文件服务器路径

    @ColumnInfo(name = "upload_retry_count")
    var uploadRetryCount: Int = 0,//文件上传重试次数

    @ColumnInfo(name = "report_retry_count")
    var reportRetryCount: Int = 0,//保存数据上报重试次数

    @ColumnInfo(name = "error_message")
    var errorMessage: String? = null,//错误信息

    @ColumnInfo(name = "product")
    var product: String? = null,

    @ColumnInfo(name = "order_id")
    var orderId: String? = null,

    @ColumnInfo(name = "mobile_type")
    var mobileType: String? = null,

    @ColumnInfo(name = "user_id")
    var userId: String? = null,

    @ColumnInfo(name = "call_log")
    var callLog: String? = null
)

// 扩展函数用于实体与领域模型的转换
fun CallTaskEntity.toCallTask(): CallTask {
    return CallTask(
        id = this.id,
        phoneNumber = this.phoneNumber,
        status = CallTask.CallStatus.valueOf(this.status),
        createTime = this.createTime,
        callStartTime = this.callStartTime,
        callEndTime = this.callEndTime,
        localAudioPath = this.localAudioPath,
        serverAudioPath = this.serverAudioPath,
        uploadRetryCount = this.uploadRetryCount,
        reportRetryCount = this.reportRetryCount,
        errorMessage = this.errorMessage,
        product = this.product,
        orderId = this.orderId,
        mobileType = this.mobileType,
        userId = this.userId,
        callLog = this.callLog,
    )
}

fun CallTask.toCallTaskEntity(): CallTaskEntity {
    return CallTaskEntity(
        id = this.id,
        phoneNumber = this.phoneNumber,
        status = this.status.name,
        createTime = this.createTime,
        callStartTime = this.callStartTime,
        callEndTime = this.callEndTime,
        localAudioPath = this.localAudioPath,
        serverAudioPath = this.serverAudioPath,
        uploadRetryCount = this.uploadRetryCount,
        reportRetryCount = this.reportRetryCount,
        errorMessage = this.errorMessage,
        product = this.product,
        orderId = this.orderId,
        mobileType = this.mobileType,
        userId = this.userId,
        callLog = this.callLog,
    )
}